#include "OpenglApp.h"
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#include <mmsystem.h>
#endif
#include "imgui.h"
#include "imgui_internal.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "ImGuiFileDialog.h"
#include "IconsFontAwesome5.h"
#include "misc/cpp/imgui_stdlib.h"
#include <iostream>
#include <assert.h>
#include "StringHash.h"
#include <fmt/core.h>
#include "FileSystem.h"
#include "Context.h"
#include "File.h"

GfxConfiguration config{ 960, 540, u8"OpenGL" };
BaseApplication* app = static_cast<BaseApplication*>(new OpenglApp(config));

int OpenglApp::Initialize()
{
	auto fileSystem = context_->GetFileSystem();
	std::string curdir = fileSystem->GetCurrentDir();
	std::string resourcePath = curdir + "/../../Resources";
	std::cout << resourcePath << std::endl;
	fileSystem->addSearchePath(resourcePath);
	resourcePath = curdir + "/../Resources";
	std::cout << resourcePath << std::endl;
	fileSystem->addSearchePath(resourcePath);

	GLFWApplication::Initialize();

	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO& io = ImGui::GetIO(); (void)io;
	io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;       // Enable Keyboard Controls

	ImGui_ImplGlfw_InitForOpenGL(window_, true);
	ImGui_ImplOpenGL3_Init("#version 150");

	ImGuiStyle& style = ImGui::GetStyle();
	style.WindowMenuButtonPosition = ImGuiDir_Right;

	io.Fonts->AddFontDefault();
	std::shared_ptr<File> ttf = fileSystem->GetFile("Fonts/Default.ttf");
	if (ttf)
	{
		ttf->Open();
		char* data = new char[ttf->GetSize()];
		ttf->Read(data, ttf->GetSize());

		ImFontConfig font_cfg = ImFontConfig();
		ImFont* font = io.Fonts->AddFontFromMemoryTTF(data, ttf->GetSize(), 20, &font_cfg, io.Fonts->GetGlyphRangesChineseFull());
		ImGui::GetIO().FontDefault = font;
	}

	std::shared_ptr<File> ttfIcon = fileSystem->GetFile("Fonts/fa-solid-900.ttf");
	static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
	if (ttfIcon && ttfIcon->Open()) {
		char* data = new char[ttfIcon->GetSize()];
		ttfIcon->Read(data, ttfIcon->GetSize());

		ImFontConfig config;
		config.MergeMode = true;
		io.Fonts->AddFontFromMemoryTTF(data, ttfIcon->GetSize(), 20, &config, icon_ranges);
	}

	return 0;
}

void OpenglApp::Finalize()
{
	GLFWApplication::Finalize();
	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();
}

void OpenglApp::Tick()
{
	GLFWApplication::Tick();
	ImGui_ImplOpenGL3_NewFrame();
	ImGui_ImplGlfw_NewFrame();
	ImGui::NewFrame();

	ImGuiContext& g = *GImGui;
	ImGuiIO& io = ImGui::GetIO();

	ImGui::Render();
	int display_w, display_h;
	ImVec4 clear_color = ImVec4(0.2f, 0.2f, 0.2f, 1.00f);
	glfwGetFramebufferSize(window_, &display_w, &display_h);
	
	glViewport(0, 0, display_w, display_h);
	glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
	glClear(GL_COLOR_BUFFER_BIT);

	ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

void OpenglApp::onSizeChange(int width, int height)
{

}